<?php
session_start();
if (!isset($_SESSION["acc_logined"]) || "no" == $_SESSION["acc_logined"]) {
    header("location:/accHestia/src/view/feature/login/index.php");
}
require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/acc_conn.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/rpc_com.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/fea/accMisc.php';

function is_param_valid($data, &$res) {
    if(!isset($data["cond"])) {
        return true;
    }
    $condcnt = count($data["cond"]);
    if(0 == $condcnt) {
        return true;
    }

    $valid_k = array("flowNo", "vochDate", "Zname", "vochNo", "attachCnt", "summary", "accId", "accName", "de", "cr");
    $valid_c_int = array("=", ">", "<");
    $valid_c_text = array("cn", "nc", "bw", "bn", "ew", "en");
    

    $y = -1; $m = -1; $d = -1;
    for($i = 0; $i < $condcnt; ++$i) {
        $kid = $data["cond"][$i]["kid"];
        $k = $data["cond"][$i]["k"];
        $c = isset($data["cond"][$i]["c"]) ? $data["cond"][$i]["c"] : ""; // not seted when qvalue is select
        $v = $data["cond"][$i]["v"];

        switch($k) {
        case "flowNo":
        case "vochNo":
        case "attachCnt":
        case "accId":
        case "de":
        case "cr":
            if(!in_array($c, $valid_c_int)) {
                set_res_NG($res, $k . " invalid", "invalid copare option:" . $c, $kid);
                return false;
            }
            if(!is_numeric($v)) {
                set_res_NG($res, $k . " invalid", "invalid number:" . $k, $kid);
                return false;
            }
            break;

        case "vochDate":
            if(!in_array($c, $valid_c_int)) {
                set_res_NG($res, $k . " invalid", "invalid copare option:" . $c, $kid);
                return false;
            }
            sscanf($v, "%d-%d-%d", $y, $m, $d);
            if(!checkdate($m, $d, $y)) { //if invalid date
                set_res_NG($res, $k . " invalid", "invalid date:" . $k, $kid);
                return false;
            }
            break;

        case "zID":
            if(!is_numeric($v)) {
                set_res_NG($res, $k . " invalid", "invalid number:" . $k, $kid);
                return false;
            }
            if($v < 1 || $v > 5) { //now valid is:1~5
                set_res_NG($res, $k . " invalid", "invalid query value:" . $v . ". (1-5 is valid.)", $kid);
                return false;
            }
            break;
        case "summary":
//        case "accName":
            if(!in_array($c, $valid_c_text)) {
                set_res_NG($res, $k . " invalid", "invalid copare option:" . $c, $kid);
                return false;
            }
            break;

        default:
            set_res_NG($res, $k . " invalid", "invalid query key:" . $k);
            return false;
        }
    }
    return true;
}

function make_query_sql_condition($data, &$cond_sql) {
    if(!isset($data["cond"])) {
        return true;
    }
    $condcnt = count($data["cond"]);
    if(0 == $condcnt) {
        return true;
    }
    $ret_cond = "";
    for($i = 0; $i < $condcnt; ++$i) {
        $kid = $data["cond"][$i]["kid"];
        $k = $data["cond"][$i]["k"];
        $c = isset($data["cond"][$i]["c"]) ? $data["cond"][$i]["c"] : ""; // not seted when qvalue is select
        $v = $data["cond"][$i]["v"];

        switch($k) {
        case "flowNo":
        case "vochNo":
        case "attachCnt":
        case "accId":
        case "de":
        case "cr":
            $ret_cond .= $k . $c . $v . " and "; 
            break;

        case "vochDate":
            sscanf($v, "%d-%d-%d", $y, $m, $d);
            $ret_cond .= "iDate" . $c . ($y * 12 * 31 + $m * 31 + $d) . " and ";
            break;

        case "zID":
            $ret_cond .= $k . "=" . $v . " and "; 
            break;
        case "summary":
//        case "accName":
            switch($c) { //"cn", "nc", "bw", "bn", "ew", "en"
            case "cn": //contain
                $ret_cond .= $k . " like '%" . SQLite3::escapeString($v) . "%' and ";
                break;
            case "nc": // not contain
                $ret_cond .= $k . " not like '%" . SQLite3::escapeString($v) . "%' and ";
                break;
            case "bw": // begin with
                $ret_cond .= $k . " like '" . SQLite3::escapeString($v) . "%' and ";
                break;
            case "bn": // begin not with
                $ret_cond .= $k . "not like '" . SQLite3::escapeString($v) . "%' and ";
                break;
            case "ew": // end with
                $ret_cond .= $k . " like '%" . SQLite3::escapeString($v) . "' and ";
                break;
            case "en": // end not with
                $ret_cond .= $k . "not like '%" . SQLite3::escapeString($v) . "' and "; //sqlite_escape_string
                break;
            default:
                break;
            }
            break;
        default:
            break;
        }
    }
    $cond_sql = preg_replace("/(.*) and $/", "$1", $ret_cond);
    return true;
}

function dorpc_qvochbase($data, &$res) {
    if(!is_param_valid($data, $res)) {
        return;
    }
    $cond = "";
    $c_ret = make_query_sql_condition($data, $cond);
    if(!$c_ret) {
        set_res_NG($res, "make condition sql failed.", "make condition sql failed.");
        return;
    }
    if("" != $cond) {
        $cond = "where " . $cond;
    }

    $pdb = new acc_DB();
    $ssql = "select * from daybook " . $cond . " order by flowNo, Zname, input_idx";
    $rs = $pdb->q($ssql);
    $res["grid_data"] = array();
    $i = 0;
    while($row = $rs->fetchArray(SQLITE3_ASSOC)) {
        $res["grid_data"][$i]['id'] = $i;
        $res["grid_data"][$i]['flowNo'] = $row["flowNo"];
        $res["grid_data"][$i]['vochDate'] = sprintf("%04d-%02d-%02d", $row["Date_y"], $row["Date_m"], $row["Date_d"]);
        $res["grid_data"][$i]['Zname'] = $row["Zname"];
        $res["grid_data"][$i]['vochNo'] = $row["vochNo"];
        $res["grid_data"][$i]['attachCnt'] = $row["attachCnt"];
        
        $res["grid_data"][$i]['makerName'] = $row["makerName"];
        $res["grid_data"][$i]['reviwerName'] = $row["reviwerName"];
        $res["grid_data"][$i]['bookerName'] = $row["bookerName"];
        $res["grid_data"][$i]['accManagerName'] = $row["accManagerName"];
        
        $res["grid_data"][$i]['summary'] = $row["summary"];
        $res["grid_data"][$i]['serialNo'] = $row["serialNo"];
        $res["grid_data"][$i]['accName'] = $row["accName"];

        $res["grid_data"][$i]['saleManName'] = $row["auxSaleManName"];
        $res["grid_data"][$i]['Prj1'] = $row["auxPrj1Name"];
        $res["grid_data"][$i]['Prj2'] = $row["auxPrj2Name"];
        $res["grid_data"][$i]['Prj3'] = $row["auxPrj3Name"];
        $res["grid_data"][$i]['auxCurrent'] = $row["auxCurrentName"];
        $res["grid_data"][$i]['accWay'] = $row["accWayName"];
        $res["grid_data"][$i]['accCloseNo'] = $row["accCloseNo"];
        $strDate = "";
        convert_iDates_to_strDate($row["accCloseDate"], $strDate);
        $res["grid_data"][$i]['accCloseDate'] = $strDate;

        $res["grid_data"][$i]['de'] = $row["de"];
        $res["grid_data"][$i]['cr'] = $row["cr"];
        $i++;
    }
    set_res_OK($res);
}

?>